// Copyright (C) Kumo inc. and its affiliates.
// Author: Jeff.li lijippy@163.com
// All rights reserved.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
//

package handler

import "net/http"

type statusError interface {
	StatusCode() int
	Status() string
	Error() string
}

type handlerError struct {
	code    int
	status  string
	message string
}

func newHandlerError(code int, status, message string) statusError {
	return &handlerError{
		code:    code,
		status:  status,
		message: message,
	}
}

func (e *handlerError) StatusCode() int {
	return e.code
}

func (e *handlerError) Status() string {
	return e.status
}

func (e *handlerError) Error() string {
	return e.message
}

var (
	// ErrorSessionMissing indicates that the specified session not found
	ErrorSessionMissing = newHandlerError(http.StatusNotFound, "SESSION NOT FOUND", "session with specified identity not found")
	// ErrorManifestMissing indicates that the specified component doesn't have manifest yet
	ErrorManifestMissing = newHandlerError(http.StatusNotFound, "MANIFEST NOT FOUND", "that component doesn't have manifest yet")
	// ErrorInvalidTarball indicates that the tarball is not valid (eg. too large)
	ErrorInvalidTarball = newHandlerError(http.StatusBadRequest, "INVALID TARBALL", "the tarball content is not valid")
	// ErrorInvalidManifest indicates that the manifest is not valid
	ErrorInvalidManifest = newHandlerError(http.StatusBadRequest, "INVALID MANIFEST", "the manifest content is not valid")
	// ErrorInternalError indicates that an internal error happened
	ErrorInternalError = newHandlerError(http.StatusInternalServerError, "INTERNAL ERROR", "an internal error happened")
	// ErrorManifestConflict indicates that the uploaded manifest is not new enough
	ErrorManifestConflict = newHandlerError(http.StatusConflict, "MANIFEST CONFLICT", "the manifest provided is not new enough")
	// ErrorForbiden indicates that the user can't access target resource
	ErrorForbiden = newHandlerError(http.StatusForbidden, "FORBIDDEN", "permission denied")
)
